import re
import time
import pymongo
import constant as c
import common.gyblog as log
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService

LOG = log.get_logger()
SERVICE = ChromeService()


# 查询是否在数据库中，没有则插入
def check_and_save(info_dict):
    # 连接数据库
    db_client = pymongo.MongoClient(c.CONNECTION_STR)
    db_database = db_client[c.DATA_BASE]
    db_col = db_database['download_sukebei']

    query = {'tor': info_dict['tor']}
    query_ans = list(db_col.find(query))

    if len(query_ans) == 0:
        db_col.insert_one(info_dict)
    elif len(query_ans) == 1:
        LOG.debug('{}已存在'.format(info_dict['tor']))
        print('{}已存在'.format(info_dict['tor']))
    else:
        print('数据重复')

    db_client.close()


def get_page_info(keyword, i):
    try:
        url = 'https://sukebei.nyaa.si/?f=2&c=0_0&q={}&p={}'.format(keyword, str(i))
        # 创建 WebDriver 对象
        driver = webdriver.Chrome(service=SERVICE)
        driver.get(url)
        categories = driver.find_elements(By.XPATH, '/html/body/div/div[2]/table/tbody/tr[position()]/td[1]/a')
        names = driver.find_elements(By.XPATH, '/html/body/div/div[2]/table/tbody/tr[position()]/td[2]/a')
        tors = driver.find_elements(By.XPATH, '/html/body/div/div[2]/table/tbody/tr[position()]/td[3]/a[2]')
        current_date = time.strftime("%Y-%m-%d", time.localtime())
        print('URL:{},共抓取到{}条信息'.format(url, len(tors)))
        LOG.info('URL:{},共抓取到{}条信息'.format(url, len(tors)))
        for i in range(0, len(tors)):
            category_text = str(categories[i].get_attribute('title'))
            category = category_text.split('-')[-1].strip()
            name = str(names[i].get_attribute('title'))
            tor_text = str(tors[i].get_attribute('href'))
            tor = re.findall("magnet:\?xt=urn:.*?&dn", tor_text)[0]
            info_dict = {'category': category, 'name': name, 'tor': tor, 'keyword': keyword,
                         'download_date': current_date,
                         'is_download': 'N'}
            LOG.debug(info_dict)
            check_and_save(info_dict)
        driver.close()
        return 1
    except:
        print('抓取失败')
        return 0


def get_info(keyword, page):
    for i in range(1, page):
        while True:
            ans = get_page_info(keyword, i)
            if ans == 1:
                break


if __name__ == '__main__':
    # keywords = ['C103', 'C102', 'C101', 'C100', 'C99', 'C98', 'C97', 'C96', 'C95']
    keywords = ['FC2']
    page = 2    # 取值范围2-15
    for keyword in keywords:
        get_info(keyword, page)

